package com.gs.SynPatrolRep.control;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.gs.SynPatrolRep.R;

public class LabelText extends LinearLayout {
    private Integer valId;
    private TextView item_lbl;
    private TextView item_text;
    private ImageView item_arrow_right;

    public Integer getValId() {
        return valId;
    }

    public void setValId(Integer valId) {
        this.valId = valId;
    }

    public String getLbl() {
        return item_lbl.getText().toString();
    }

    public void setLbl(String text) {
        item_lbl.setText(text);
    }

    public void setLblHint(String hint) {
        item_lbl.setHint(hint);
    }

    public String getText() {
        return item_text.getText().toString();
    }

    public void setText(String text) {
        if (text != null) {
            item_text.setText(text);
        } else {
            item_text.setText("");
        }

    }

    public void setTextHint(String hint) {
        item_text.setHint(hint);
    }

    public LabelText(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.LabelText);
        LayoutInflater.from(context).inflate(R.layout.label_text, this, true);
        item_lbl = findViewById(R.id.item_lbl);
        item_text = findViewById(R.id.item_text);
        item_arrow_right = findViewById(R.id.item_arrow_right);
        String text = attributes.getString(R.styleable.LabelText_label_text);
        String textHint = attributes.getString(R.styleable.LabelText_label_text_hint);
        boolean isStar = attributes.getBoolean(R.styleable.LabelText_label_text_star, false);
        item_text.setText(text);
        item_text.setText("");
        item_text.setHint(textHint);
        String textVisibility = attributes.getString(R.styleable.LabelText_label_text_visibility);
        if (textVisibility != null && !TextUtils.isEmpty(textVisibility)) {
            switch (textVisibility) {
                case "visible":
                    item_text.setVisibility(VISIBLE);
                    break;
                case "invisible":
                    item_text.setVisibility(INVISIBLE);
                    break;
                case "gone":
                    item_text.setVisibility(GONE);
                    break;
            }
        }
        String lbl = attributes.getString(R.styleable.LabelText_label_text_lbl);
        if (isStar) {
            Spannable spannable = new SpannableString("* " + lbl);
            spannable.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.deeper_red)), 0, 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
            item_lbl.setText(spannable);
        } else {
            item_lbl.setText(lbl);
        }
        String lblVisibility = attributes.getString(R.styleable.LabelText_label_text_lbl_visibility);
        if (lblVisibility != null && !TextUtils.isEmpty(lblVisibility)) {
            switch (lblVisibility) {
                case "visible":
                    item_lbl.setVisibility(VISIBLE);
                    break;
                case "invisible":
                    item_lbl.setVisibility(INVISIBLE);
                    break;
                case "gone":
                    item_lbl.setVisibility(GONE);
                    break;
            }
        }
        String arrowRightVisibility = attributes.getString(R.styleable.LabelText_label_text_arrow_right);
        if (arrowRightVisibility != null && !TextUtils.isEmpty(arrowRightVisibility)) {
            switch (arrowRightVisibility) {
                case "visible":
                    item_arrow_right.setVisibility(VISIBLE);
                    break;
                case "invisible":
                    item_arrow_right.setVisibility(INVISIBLE);
                    break;
                case "gone":
                    item_arrow_right.setVisibility(GONE);
                    break;
            }
        }
        int maxLines = attributes.getInt(R.styleable.LabelText_label_text_maxLines, 0);
        if (maxLines != 0) {
            item_text.setMaxLines(maxLines);
        }
        String ellipsize = attributes.getString(R.styleable.LabelText_label_text_ellipsize);
        if (ellipsize != null || !TextUtils.isEmpty(ellipsize)) {
            item_text.setEllipsize(TextUtils.TruncateAt.valueOf(ellipsize));
        }
        int maxEms = attributes.getInt(R.styleable.LabelText_label_text_maxEms, 0);
        if (maxEms != 0) {
            item_text.setMaxEms(maxEms);
        }

        attributes.recycle();
    }
}
